package com.oro.domain;

import static javax.persistence.CascadeType.ALL;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.hibernate.validator.NotNull;

/**
 * Entity implementation class for Entity: Traduttore
 * 
 */
@Entity
public class Traduttore extends Servizio implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private String citizenship;
	
	private String passportNumber;
	@Transient
	private Email emailToRemove;
	@Transient
	private Telephone phoneToRemove;
	@Transient
	private IndirizzoAU indAuToRemove;
	@Transient
	private IndirizzoIT indItToRemove;
	
	public void addEmptyPhone() {
		getTelephones().add(new Telephone());
	}
	
	public void addEmptyEmail() {
		getEmails().add(new Email());
	}
	
	public void addEmptyIndIt() {
		getIndirizziIt().add(new IndirizzoIT());
	}
	
	public void addEmptyIndAu() {
		getIndirizziAu().add(new IndirizzoAU());
	}
	
	public void removeEmail() {
		getEmails().remove(emailToRemove);
	}
	
	public void removeTelephone() {
		getTelephones().remove(phoneToRemove);
	}
	
	public void removeIndIt() {
		getIndirizziIt().remove(indItToRemove);
	}
	
	public void removeIndAu() {
		getIndirizziAu().remove(indAuToRemove);
	}

	@OneToMany(cascade = ALL)
	@JoinTable
	private List<Email> emails;

	@OneToMany(cascade = ALL)
	@JoinTable
	private List<Fax> faxes;

	@NotNull
	private String firstName;

	@OneToMany(cascade = ALL)
	@JoinTable
	private List<IndirizzoAU> indirizziAu;

	@OneToMany(cascade = ALL)
	@JoinTable
	private List<IndirizzoIT> indirizziIt;

	private String middleName;

	private String registrationNumber;

	@NotNull
	private String surname;

	@OneToMany(cascade = ALL)
	@JoinTable
	private List<Telephone> telephones;

	private String title;

	@OneToMany(mappedBy = "translator", cascade = ALL)
	private List<TranslatorReferral> translatorReferrals;

	public Traduttore() {
		super();
		emails = new ArrayList<Email>();
		faxes = new ArrayList<Fax>();
		telephones = new ArrayList<Telephone>();
		indirizziIt = new ArrayList<IndirizzoIT>();
		indirizziAu = new ArrayList<IndirizzoAU>();
		translatorReferrals = new ArrayList<TranslatorReferral>();
	}

	public boolean add(Email email) {
		// return emails.add(email); // this does not work
		if (this.has(email))
			return false;
		emails.add(email);
		return true;
	}

	public boolean add(Fax fax) {
		if (this.has(fax))
			return false;
		faxes.add(fax);
		return true;
	}

	public boolean add(Telephone tel) {
		if (this.has(tel))
			return false;
		telephones.add(tel);
		return true;
	}

	@Override
	public boolean equals(Object o) {
		if (o == null)
			return false;
		if (!(o instanceof MigrationAgent))
			return false;
		Traduttore ma = (Traduttore) o;
		if (this == ma)
			return true;
		if (getId() != null && getId() != 0L)
			return getId().equals(ma.getId());
		else {
			if (getRegistrationNumber() != null)
				return getRegistrationNumber().equals(
						ma.getRegistrationNumber());
			else
				return false;
		}
	}

	public int getEmailNumber() {
		return emails.size();
	}

	public List<Email> getEmails() {
		return emails;
	}

	public List<Fax> getFaxes() {
		return faxes;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public String getRegistrationNumber() {
		return registrationNumber;
	}

	public String getSurname() {
		return surname;
	}

	public List<Telephone> getTelephones() {
		return telephones;
	}

	public String getTitle() {
		return title;
	}

	public List<TranslatorReferral> getTranslatorReferrals() {
		return translatorReferrals;
	}

	public boolean has(Email au) {
		return emails.contains(au);
	}

	public boolean has(Fax fax) {
		return faxes.contains(fax);
	}

	public boolean has(Telephone tel) {
		return telephones.contains(tel);
	}

	public boolean hasAll(Collection<Email> emails) {
		return this.emails.containsAll(emails);
	}

	public boolean hasEmail(String email) {
		for (Email e : emails)
			if (e.getEmail().equals(email))
				return true;
		return false;
	}

	public boolean hasFax(String number) {
		for (Fax f : faxes)
			if (f.getNumber().equals(number))
				return true;
		return false;
	}

	public boolean hasTelephone(String number) {
		for (Telephone t : telephones)
			if (t.getNumber().equals(number))
				return true;
		return false;
	}

	public boolean remove(Email email) {
		return emails.remove(email);
	}

	public boolean remove(Fax fax) {
		return faxes.remove(fax);
	}

	public boolean remove(Telephone tel) {
		return telephones.remove(tel);
	}

	public void setEmails(List<Email> emails) {
		this.emails = emails;
	}

	public void setFaxes(List<Fax> faxes) {
		this.faxes = faxes;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public void setRegistrationNumber(String registrationNumber) {
		this.registrationNumber = registrationNumber;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public void setTelephones(List<Telephone> telephones) {
		this.telephones = telephones;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setTranslatorReferrals(
			List<TranslatorReferral> translatorReferrals) {
		this.translatorReferrals = translatorReferrals;
	}

	public void setCitizenship(String citizenship) {
		this.citizenship = citizenship;
	}

	public String getCitizenship() {
		return citizenship;
	}

	public void setPassportNumber(String passportNumber) {
		this.passportNumber = passportNumber;
	}

	public String getPassportNumber() {
		return passportNumber;
	}

	public Email getEmailToRemove() {
		return emailToRemove;
	}

	public void setEmailToRemove(Email emailToRemove) {
		this.emailToRemove = emailToRemove;
	}

	public Telephone getPhoneToRemove() {
		return phoneToRemove;
	}

	public void setPhoneToRemove(Telephone phoneToRemove) {
		this.phoneToRemove = phoneToRemove;
	}

	public IndirizzoAU getIndAuToRemove() {
		return indAuToRemove;
	}

	public void setIndAuToRemove(IndirizzoAU indAuToRemove) {
		this.indAuToRemove = indAuToRemove;
	}

	public IndirizzoIT getIndItToRemove() {
		return indItToRemove;
	}

	public void setIndItToRemove(IndirizzoIT indItToRemove) {
		this.indItToRemove = indItToRemove;
	}

	public void setIndirizziAu(List<IndirizzoAU> indirizziAu) {
		this.indirizziAu = indirizziAu;
	}

	public List<IndirizzoAU> getIndirizziAu() {
		return indirizziAu;
	}

	public void setIndirizziIt(List<IndirizzoIT> indirizziIt) {
		this.indirizziIt = indirizziIt;
	}

	public List<IndirizzoIT> getIndirizziIt() {
		return indirizziIt;
	}
}
